/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package PDPictureEditor;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import GuiTools.GuiTools;
import GuiTools.Header;
import GuiTools.MDTribuitFrame;
import GuiTools.TribuitFrame;
import PDPictureEditor.PictureEditorDialog.PNUPanel;
import PictureDescription.*;
import Settings.*;

/**
 * @author Christoph Schnack
 *
 */
public class PicturePanel extends JPanel{
	Image image = null;
	ImagePanel imagePanel = null;
	Settings SET;
	Language LANG;
	boolean setTag,highlighting,highlightSwitch,markTag,highlightVector;
	TribuitFrame parentFrame;
	JScrollPane scrollPane;
	int hlIndex,setTagIndex,markTagIndex,selectedTag,hlVectorNumber;
	GridBagConstraints gbc;
	PicturePanelParent PED;
	Color tadeColor,tadeFlashColor;
	String tadeType;
	//edit PTU
	int editIndex,editTagIndex;
	double zoom = 1.0;
	PictureTagUnit editPtu;
	boolean resetPtuPoint,setTagAreaDefinitionElement;
	Cursor 
	addTADECursor = Toolkit.getDefaultToolkit().createCustomCursor(GuiTools.loadImageFromGraphics("addTADECursor.png"),new Point(0, 0), "FistCursor"),
	tagCursor = Toolkit.getDefaultToolkit().createCustomCursor(GuiTools.loadImageFromGraphics("tagCursor.png"),new Point(0, 0), "FistCursor"),
	resetTagCursor = Toolkit.getDefaultToolkit().createCustomCursor(GuiTools.loadImageFromGraphics("resetTagCursor.png"),new Point(0, 0), "FistCursor"),
	defaultCursor = Cursor.getDefaultCursor();
	
	public PicturePanel(Settings set,TribuitFrame frame,PicturePanelParent ped){
		super();
		SET=set;
		LANG=SET.getLanguage();
		parentFrame=frame;
		PED=ped;
		imagePanel=new ImagePanel();
		scrollPane=new JScrollPane(imagePanel);
		scrollPane.setPreferredSize(new Dimension(400,500));
		scrollPane.getVerticalScrollBar().setUnitIncrement(50);
		scrollPane.getHorizontalScrollBar().setUnitIncrement(50);
		JPanel emptyPanel = new JPanel();
		emptyPanel.setBorder(null);
		JPanel emptyPanel2 = new JPanel();
		emptyPanel2.setBorder(null);
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.add(scrollPane);
		this.setBorder(null);
		imagePanel.setBorder(null);
		scrollPane.setBorder(null);
	}
	
	public void setImage(Image img){
		if(image!=null){image.flush();}
		image=img;
		setZoom(zoom);
	}
	
	public void reset(){
		resetPtuPoint=false;
		setTagAreaDefinitionElement=false;
		setTag=false;
		highlightVector=false;
		imagePanel.setCursor(defaultCursor);
	}
	
	private class ImagePanel extends JPanel{
		public ImagePanel(){
			super();
			this.addMouseListener(new MouseListener(){
				public void mouseClicked(MouseEvent arg0) {}
				public void mouseEntered(MouseEvent arg0) {}
				public void mouseExited(MouseEvent arg0) {}

				public void mousePressed(MouseEvent arg) {
					if(arg.getButton()==3){
						setTag=false;
						resetPtuPoint=false;
						setTagAreaDefinitionElement=false;
						imagePanel.setCursor(defaultCursor);
					}
					if(arg.getButton()==1){
						Point clickPoint = new Point((int)Math.round(arg.getPoint().x/zoom),(int)Math.round(arg.getPoint().y/zoom));
						if(resetPtuPoint){
							resetPtuPoint=false;
							editPtu.setCenterPosition(clickPoint);
							PED.setPictureTagUnit(editIndex,editTagIndex,editPtu);
							imagePanel.setCursor(defaultCursor);
							repaintImagePanel();
						}
						else{
							if(setTag){
								new CreateTagDialog(clickPoint.x,clickPoint.y);
							}
							else{
								if(setTagAreaDefinitionElement){
									editPtu.addPictureTagAbsolute(tadeType,clickPoint,tadeColor,tadeFlashColor);
									PED.setPictureTagUnit(editIndex,editTagIndex,editPtu);
									repaintImagePanel();
									String[] opts = {LANG.getYes(),LANG.getNo()};
									int dialog = JOptionPane.showOptionDialog(parentFrame, LANG.getAddAnotherTagAreaDefinitionElement(), LANG.getQuestion(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, opts, LANG.getYes());
									if(dialog==JOptionPane.YES_OPTION){
										SwingUtilities.invokeLater(new Runnable(){
											public void run(){
												new TagArmProperties();
											}
										});
									}
									else{
										imagePanel.setCursor(defaultCursor);
										setTagAreaDefinitionElement=false;
									}
								}
							}
						}
					}
				}
				public void mouseReleased(MouseEvent arg0) {}
			});
		}
	
		public void paintComponent(Graphics g){
			if(image!=null){
				ImageIcon ii = new ImageIcon(image);
				int width=(int)Math.round(ii.getIconWidth()*zoom),
				height=(int)Math.round(ii.getIconHeight()*zoom);
				g.clearRect(0,0,imagePanel.getWidth(),imagePanel.getHeight());
				Graphics2D g2 = (Graphics2D) g;
				g2.drawImage(image,0,0,width,height,imagePanel);
				Vector<PNUComponent> pictureNameUnits= PED.getPictureNameUnits();
				if(!pictureNameUnits.isEmpty()){
					PNUComponent element;
					for(int i=0;i<pictureNameUnits.size();i++){
						element = pictureNameUnits.elementAt(i);
						if(!element.getHideTags() || PED.getShowAllTags()){
							if(markTag && (element.getIndex()==markTagIndex)){
								if((element.getIndex()==hlIndex)&&highlighting) new DrawTags(SET,g,element.getIndex(),element.getPictureTags(),highlightSwitch,zoom,highlightVector,hlVectorNumber,true,selectedTag);
								else new DrawTags(SET,g,element.getIndex(),element.getPictureTags(),false,zoom,highlightVector,hlVectorNumber,true,selectedTag);
							}
							else{
								if((element.getIndex()==hlIndex)&&highlighting) new DrawTags(SET,g,element.getIndex(),element.getPictureTags(),highlightSwitch,zoom);
								else new DrawTags(SET,g,element.getIndex(),element.getPictureTags(),false,zoom);
							}
						}
					}
				}
			}
			else super.paintComponent(g);
		}
	}
	
	public void repaintImagePanel(){
		imagePanel.repaint();
	}
	
	public void setTag(boolean arg){
		setTag=arg;
		imagePanel.setCursor(tagCursor);
	}
	
	public void setTagIndex(int arg){
		setTagIndex=arg;
	}
	
	public void enableMarkTag(int index,int tag){
		markTag=true;
		markTagIndex=index;
		selectedTag=tag;
	}
	
	public void disableMarkTag(){
		markTag=false;
	}
	
	public void setHighlighting(boolean arg){
		highlighting=arg;
	}
	
	public void setHighlightSwitch(boolean arg){
		highlightSwitch=arg;
	}
	
	public void setHighlightVector(boolean arg){
		highlightVector = arg;
	}
	
	public boolean getHighlightVector(){
		return highlightVector;
	}
	
	public void setHighlightVectorNumber(int nr){
		hlVectorNumber = nr;
	}
	
	public void setHlIndex(int arg){
		hlIndex=arg;
	}
	
	public void setZoom(double z){
		zoom = z;
		if(image!=null){
			ImageIcon ii = new ImageIcon(image);
			int width=(int)Math.round(ii.getIconWidth()*zoom),
			height=(int)Math.round(ii.getIconHeight()*zoom);
			imagePanel.setPreferredSize(new Dimension(width,height));
			scrollPane.setMaximumSize(new Dimension(width,height));
			imagePanel.revalidate();
			parentFrame.setPreferredSize(new Dimension(parentFrame.getWidth(),parentFrame.getHeight()));
			parentFrame.pack();
			parentFrame.repaint();
		}
	}
	
	
	public void createTagDialog(int x, int y){
		new CreateTagDialog(x,y);
	}
	
	public void createTagDialog(int ind, int tagInd, PictureTagUnit p){
		new CreateTagDialog(ind, tagInd, p);
	}
	
	public void createTagArmDialog(int editInd,int editTagInd,PictureTagUnit editP){
		editIndex = editInd;
		editTagIndex = editTagInd;
		editPtu = editP;
		setTagAreaDefinitionElement=true;
		new  TagArmProperties();
	}
	/**
	 * Dialog for setting a tag's properties. Runnable either in creating or editing mode.
	 * 
	 * @author Christoph Schnack
	 *
	 */
	
	private class CreateTagDialog extends JDialog{
		JDialog dialog;
		int pointX,pointY;
		Color bgColor=Color.white,bgHlColor=Color.red,shapeColor=Color.black;
		JPanel shapePreview,bgPreview,bgHlPreview;
		boolean edit=false;
		HashMap<String,String> settings;
		//for edit mode
		int index,tagIndex;
		int radius=12;
		boolean setNewPoint;
		PictureTagUnit ptu;
		JCheckBox setNewPointCB,rememberSettingsCB;
		
		/**
		 * Constructor for editing.
		 * @param ind
		 * @param tagInd
		 * @param p
		 */
		public CreateTagDialog(int ind, int tagInd,PictureTagUnit p){
			super(parentFrame,true);
			this.setTitle(LANG.getTagProperties());
			index=ind;
			tagIndex=tagInd;
			ptu=p;
			bgColor=p.getBgColor();
			shapeColor=p.getColor();
			bgHlColor=p.getBgHlColor();
			radius=p.getRadius();
			edit=true;
			buildTagDialog();
		}
		
		/**
		 * Constructor for creating.
		 * 
		 * @param x Position x
		 * @param y Position y
		 */
		public CreateTagDialog(int x, int y){
			super(parentFrame,true);
			this.setTitle(LANG.getTagProperties());
			pointX=x;
			pointY=y;
			buildTagDialog();
		}
		
		public void buildTagDialog(){
			dialog=this;
			settings = SET.getCreateTagSettings();
			final JRadioButton smallRB = new JRadioButton(LANG.getSmall()+"(16px)");
			smallRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					radius = 8;
				}
			});
			final JRadioButton mediumRB = new JRadioButton(LANG.getMedium()+"(24px)",true);
			mediumRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					radius = 12;
				}
			});
			final JRadioButton bigRB = new JRadioButton(LANG.getBig()+"(32px)");
			bigRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					radius = 16;
				}
			});
			ButtonGroup bg = new ButtonGroup();
			bg.add(smallRB);
			bg.add(mediumRB);
			bg.add(bigRB);
			JPanel sizePanel = new JPanel();
			sizePanel.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor =GridBagConstraints.NORTHWEST; 
			gbc.gridx=1;
			sizePanel.add(smallRB,gbc);
			gbc.gridx=2;
			sizePanel.add(mediumRB,gbc);
			gbc.gridx=3;
			sizePanel.add(bigRB,gbc);
			gbc.gridy=1;
			gbc.gridx=1;
			sizePanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("smallTag.png"))),gbc);
			gbc.gridx=2;
			sizePanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("mediumTag.png"))),gbc);
			gbc.gridx=3;
			sizePanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("bigTag.png"))),gbc);
			sizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),LANG.getTagSize()+":",TitledBorder.ABOVE_TOP,TitledBorder.LEFT,new Font(Font.SANS_SERIF,Font.BOLD,18)));
			switch((edit)?ptu.getRadius():Integer.parseInt(settings.get("radius"))){
				case 8: 	smallRB.doClick();
							break;
				case 12:	mediumRB.doClick();
							break;
				case 16:	bigRB.doClick();
							break;
			}
			if(!edit){
				String[] bgCol = settings.get("fillColor").split(";"),bgHlCol = settings.get("fillHlColor").split(";"),shapeCol = settings.get("shapeColor").split(";");
				bgColor = new Color(Integer.parseInt(bgCol[0]),Integer.parseInt(bgCol[1]),Integer.parseInt(bgCol[2]));
				bgHlColor = new Color(Integer.parseInt(bgHlCol[0]),Integer.parseInt(bgHlCol[1]),Integer.parseInt(bgHlCol[2]));
				shapeColor = new Color(Integer.parseInt(shapeCol[0]),Integer.parseInt(shapeCol[1]),Integer.parseInt(shapeCol[2]));
			}
			JPanel shapeColorPanel = new JPanel();
			shapeColorPanel.setBorder(BorderFactory.createTitledBorder(LANG.getShapeColor()+":"));
			JButton chooseShapeColorBtn = new JButton(LANG.getSelect());
			chooseShapeColorBtn.addActionListener(new ColorChooserListener(1));
			shapePreview = new JPanel();
			shapePreview.setPreferredSize(new Dimension(70,50));
			shapePreview.setBackground(shapeColor);
			shapeColorPanel.setLayout(new BoxLayout(shapeColorPanel,BoxLayout.Y_AXIS));
			shapeColorPanel.add(shapePreview);
			shapeColorPanel.add(chooseShapeColorBtn);
			
			JPanel bgColorPanel = new JPanel();
			bgColorPanel.setBorder(BorderFactory.createTitledBorder(LANG.getBackgroundColor()+":"));
			JButton chooseBgColorBtn = new JButton(LANG.getSelect());
			chooseBgColorBtn.addActionListener(new ColorChooserListener(2));
			bgPreview = new JPanel();
			bgPreview.setPreferredSize(new Dimension(70,50));
			bgPreview.setBackground(bgColor);
			bgColorPanel.setLayout(new BoxLayout(bgColorPanel,BoxLayout.Y_AXIS));
			bgColorPanel.add(bgPreview);
			bgColorPanel.add(chooseBgColorBtn);
			
			JPanel bgHlColorPanel = new JPanel();
			bgHlColorPanel.setBorder(BorderFactory.createTitledBorder(LANG.getFlashingColor()+":"));
			JButton chooseBgHlColorBtn = new JButton(LANG.getSelect());
			chooseBgHlColorBtn.addActionListener(new ColorChooserListener(3));
			bgHlPreview = new JPanel();
			bgHlPreview.setPreferredSize(new Dimension(70,50));
			bgHlPreview.setBackground(bgHlColor);
			bgHlColorPanel.setLayout(new BoxLayout(bgHlColorPanel,BoxLayout.Y_AXIS));
			bgHlColorPanel.add(bgHlPreview);
			bgHlColorPanel.add(chooseBgHlColorBtn);
			
			JPanel colorsPanel = new JPanel();
			colorsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),LANG.getColors()+":",TitledBorder.ABOVE_TOP,TitledBorder.LEFT,new Font(Font.SANS_SERIF,Font.BOLD,18)));
			colorsPanel.add(shapeColorPanel);
			colorsPanel.add(bgColorPanel);
			colorsPanel.add(bgHlColorPanel);
			
			setNewPointCB = new JCheckBox(LANG.getSetNewCenterPoint(),false);
			JPanel cbPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
			cbPanel.setLayout(new BoxLayout(cbPanel,BoxLayout.PAGE_AXIS));
			cbPanel.add(setNewPointCB);
			cbPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),LANG.getAfterConfirmigTagProperties()+":",TitledBorder.ABOVE_TOP,TitledBorder.LEFT,new Font(Font.SANS_SERIF,Font.BOLD,18)));
			
			rememberSettingsCB = new JCheckBox(LANG.getRememberSettings(),settings.get("rememberSettings").equals("true"));
			JButton loadDefaultsBtn = new JButton(LANG.getSetDefaults());
			loadDefaultsBtn.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					switch(Integer.parseInt(settings.get("defaultRadius"))){
					case 8: 	smallRB.doClick();
								break;
					case 12:	mediumRB.doClick();
								break;
					case 16:	bigRB.doClick();
								break;
					}
					String[] bgCol = settings.get("defaultFillColor").split(";"),bgHlCol = settings.get("defaultFillHlColor").split(";"),shapeCol = settings.get("defaultShapeColor").split(";");
					bgColor = new Color(Integer.parseInt(bgCol[0]),Integer.parseInt(bgCol[1]),Integer.parseInt(bgCol[2]));
					bgHlColor = new Color(Integer.parseInt(bgHlCol[0]),Integer.parseInt(bgHlCol[1]),Integer.parseInt(bgHlCol[2]));
					shapeColor = new Color(Integer.parseInt(shapeCol[0]),Integer.parseInt(shapeCol[1]),Integer.parseInt(shapeCol[2]));
					bgHlPreview.setBackground(bgHlColor);
					shapePreview.setBackground(shapeColor);
					bgPreview.setBackground(bgColor);
					
				}
			});
			JPanel rsPanel = new JPanel();
			rsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),LANG.getSettings()+":",TitledBorder.ABOVE_TOP,TitledBorder.LEFT,new Font(Font.SANS_SERIF,Font.BOLD,18)));
			rsPanel.add(rememberSettingsCB);
			rsPanel.add(loadDefaultsBtn);
			
			JPanel centerPanel = new JPanel();
			centerPanel.setLayout(new GridBagLayout());
			GridBagConstraints gb = new GridBagConstraints();
			gb.fill = GridBagConstraints.BOTH;
			gb.anchor = GridBagConstraints.NORTHWEST;
			gb.insets = new Insets(10,10,10,10);
			centerPanel.add(sizePanel,gb);
			gb.gridy=1;
			centerPanel.add(colorsPanel,gb);
			if(edit){
				gb.gridy=2;
				centerPanel.add(cbPanel,gb);
				rememberSettingsCB.setSelected(false);
			}
			gb.gridy=3;
			centerPanel.add(rsPanel,gb);
			centerPanel.setBackground(Color.gray);
			
			JButton okButton = new JButton("ok");
			okButton.addActionListener(new OkBtnListener());
			JButton cancelButton = new JButton(LANG.getCancel());
			cancelButton.addActionListener(new CancelBtnListener());
			JPanel buttonPanel = new JPanel();
			buttonPanel.add(okButton);
			buttonPanel.add(cancelButton);
			buttonPanel.setBackground(GuiTools.BGCOLOR);
			this.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getTagProperties()));
			this.getContentPane().add(BorderLayout.CENTER,centerPanel);
			this.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
			this.pack();
			Point p = parentFrame.getLocationOnScreen();
			this.setLocation(p.x+50,p.y+50);
			this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					closeDialog();
				}
			});
			repaintImagePanel();
			this.setVisible(true);
		}
		
		private class OkBtnListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				settings.put("rememberSettings", rememberSettingsCB.isSelected()?"true":"false");
				if(rememberSettingsCB.isSelected()){
					settings.put("radius", ""+radius);
					settings.put("shapeColor", shapeColor.getRed()+";"+shapeColor.getGreen()+";"+shapeColor.getBlue());
					settings.put("fillColor", bgColor.getRed()+";"+bgColor.getGreen()+";"+bgColor.getBlue());
					settings.put("fillHlColor", bgHlColor.getRed()+";"+bgHlColor.getGreen()+";"+bgHlColor.getBlue());
				}
				SET.setCreateTagSettings(settings);
				if(edit){
					ptu.setBgColor(bgColor);
					ptu.setColor(shapeColor);
					ptu.setBgHlColor(bgHlColor);
					ptu.setRadius(radius);
					editIndex=index;
					editTagIndex=tagIndex;
					editPtu=ptu;
					if(setNewPointCB.isSelected()){
						dialog.dispose();
						resetPtuPoint=true;
						imagePanel.setCursor(resetTagCursor);
						return;
					}
				}
				else{
					PED.addPictureTagUnitTo(new PictureTagUnit(null,new Point(pointX,pointY),shapeColor,bgColor,bgHlColor,radius),setTagIndex);
				}
				closeDialog();
			}
		}
		
		private class CancelBtnListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				closeDialog();
			}
		}
		
		public void closeDialog(){
			imagePanel.setCursor(defaultCursor);
			setTag=false;
			repaintImagePanel();
			dialog.dispose();
		}
		
		private class ColorChooserListener implements ActionListener{
			int variable;
			public ColorChooserListener(int arg){
				variable=arg;
			}
			public void actionPerformed(ActionEvent e){
				switch (variable){
					case 1:
						Color color=JColorChooser.showDialog(dialog, LANG.getShapeColor(), shapeColor);
						shapeColor=color==null?shapeColor:color;
						shapePreview.setBackground(shapeColor);
						break;
					case 2:
						Color col=JColorChooser.showDialog(dialog, LANG.getBackgroundColor(), bgColor);
						bgColor=(col==null)?bgColor:col;
						bgPreview.setBackground(bgColor);
						break;
					case 3:
						Color co=JColorChooser.showDialog(dialog, LANG.getFlashingColor(), bgHlColor);
						bgHlColor=(co==null)?bgHlColor:co;
						bgHlPreview.setBackground(bgHlColor);
						break;
				}
			}
		}
	}
	
	public void editTade(int tIndex, int aIndex,TADE tade){
		new TagArmProperties(tIndex,aIndex,tade);
	}
	
	private class TagArmProperties extends JDialog{
		JRadioButton smallDelimiter=new JRadioButton(LANG.getSmall()),mediumDelimiter=new JRadioButton(LANG.getMedium()),bigDelimiter=new JRadioButton(LANG.getBig()),smallArrow=new JRadioButton(LANG.getSmall(),true),mediumArrow=new JRadioButton(LANG.getMedium()),bigArrow=new JRadioButton(LANG.getBig());
		JDialog dialog;
		JPanel colorPreview,flashColorPreview;
		Color color,flashColor;
		String type = "smallArrow";
		JCheckBox rememberSettingsCB;
		HashMap<String,String> settings;
		TADE tade;
		int editTagIndex,editArmIndex;
		boolean edit;
		
		public TagArmProperties(int eTagIndex,int eArmIndex,TADE tadeArg){
			super(parentFrame,true);
			dialog=this;
			edit = true;
			tade=tadeArg;
			editTagIndex = eTagIndex;
			editArmIndex = eArmIndex;
			build(tade.getColor(),tade.getFlashColor());
		}
		
		public TagArmProperties(){
			super(parentFrame,true);
			dialog=this;
			build(Color.gray,Color.red);
		}
		
		public void build(Color c,Color fc){
			settings = SET.getCreateTADESettings();
			color=c;
			flashColor=fc;
			this.setTitle((edit)?LANG.getEditTagAreaDefiningElement():LANG.getAddTagAreaDefiningElement());
			ButtonGroup bg = new ButtonGroup();
			bg.add(smallArrow);
			bg.add(mediumArrow);
			bg.add(bigArrow);
			bg.add(smallDelimiter);
			bg.add(mediumDelimiter);
			bg.add(bigDelimiter);
			
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.NORTHWEST;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.gridy=1;
			gbc.gridx=1;
			
			smallArrow.addActionListener(new CbListener("smallArrow"));
			mediumArrow.addActionListener(new CbListener("mediumArrow"));
			bigArrow.addActionListener(new CbListener("bigArrow"));
			
			JPanel arrowPanel = new JPanel();
			arrowPanel.setLayout(new GridBagLayout());
			arrowPanel.setBorder(BorderFactory.createTitledBorder(LANG.getArrow()+":"));
			arrowPanel.add(smallArrow,gbc);
			gbc.gridy=2;
			arrowPanel.add(mediumArrow,gbc);
			gbc.gridy=3;
			arrowPanel.add(bigArrow,gbc);
			gbc.gridy=1;
			gbc.gridx=2;
			arrowPanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("smallArrow.png"))),gbc);
			gbc.gridy=2;
			arrowPanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("mediumArrow.png"))),gbc);
			gbc.gridy=3;
			arrowPanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("bigArrow.png"))),gbc);
			
			smallDelimiter.addActionListener(new CbListener("smallDelimiter"));
			mediumDelimiter.addActionListener(new CbListener("mediumDelimiter"));
			bigDelimiter.addActionListener(new CbListener("bigDelimiter"));
			
			gbc.gridx=1;
			gbc.gridy=1;
			JPanel delimiterPanel = new JPanel();
			delimiterPanel.setLayout(new GridBagLayout());
			delimiterPanel.setBorder(BorderFactory.createTitledBorder(LANG.getDelimiter()+":"));
			delimiterPanel.add(smallDelimiter,gbc);
			gbc.gridy=2;
			delimiterPanel.add(mediumDelimiter,gbc);
			gbc.gridy=3;
			delimiterPanel.add(bigDelimiter,gbc);
			gbc.gridy=1;
			gbc.gridx=2;
			delimiterPanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("smallDelimiter.png"))),gbc);
			gbc.gridy=2;
			delimiterPanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("mediumDelimiter.png"))),gbc);
			gbc.gridy=3;
			delimiterPanel.add(new JLabel(new ImageIcon(GuiTools.loadImageFromGraphics("bigDelimiter.png"))),gbc);
			
			JPanel typePanel = new JPanel();
			typePanel.setBorder(BorderFactory.createTitledBorder(LANG.getType()+":"));
			typePanel.add(arrowPanel);
			typePanel.add(delimiterPanel);
			
			JPanel colorPanel = new JPanel();
			colorPanel.setBorder(BorderFactory.createTitledBorder(LANG.getColor()+":"));
			colorPreview = new JPanel();
			colorPreview.setPreferredSize(new Dimension(50,30));
			colorPreview.setBackground(color);
			JButton colorBtn = new JButton(LANG.getSelect());
			colorBtn.addActionListener(new ColorBtnListener());
			colorPanel.add(colorPreview);
			colorPanel.add(colorBtn);
			
			JPanel flashColorPanel = new JPanel();
			flashColorPanel.setBorder(BorderFactory.createTitledBorder(LANG.getFlashingColor()+":"));
			flashColorPreview = new JPanel();
			flashColorPreview.setPreferredSize(new Dimension(50,30));
			flashColorPreview.setBackground(flashColor);
			JButton flashColorBtn = new JButton(LANG.getSelect());
			flashColorBtn.addActionListener(new FlashColorBtnListener());
			flashColorPanel.add(flashColorPreview);
			flashColorPanel.add(flashColorBtn);
			
			JPanel colorsPanel = new JPanel();
			colorsPanel.add(colorPanel);
			colorsPanel.add(flashColorPanel);
			
			rememberSettingsCB = new JCheckBox(LANG.getRememberSettings(),settings.get("rememberSettings").equals("true"));
			JButton setDefaultsBtn = new JButton(LANG.getSetDefaults());
			setDefaultsBtn.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String type = settings.get("defaultType");
					if(type.equals("smallArrow")) smallArrow.doClick();
					if(type.equals("mediumArrow")) mediumArrow.doClick();
					if(type.equals("bigArrow")) bigArrow.doClick();
					if(type.equals("smallDelimiter")) smallDelimiter.doClick();
					if(type.equals("mediumDelimiter")) mediumDelimiter.doClick();
					if(type.equals("bigDelimiter")) bigDelimiter.doClick();
					String colo = settings.get("defaultColor");
					String[] split1 = colo.split(";");
					color = new Color(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]));
					String fColor = settings.get("defaultFlashColor");
					String[] split2 = fColor.split(";");
					flashColor = new Color(Integer.parseInt(split2[0]),Integer.parseInt(split2[1]),Integer.parseInt(split2[2]));
					colorPreview.setBackground(color);
					flashColorPreview.setBackground(flashColor);
				}
			});
			JPanel settingsPanel = new JPanel();
			settingsPanel.add(rememberSettingsCB);
			settingsPanel.add(setDefaultsBtn);
			
			JPanel centerPanel = new JPanel();
			centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));
			centerPanel.add(typePanel);
			centerPanel.add(colorsPanel);
			centerPanel.add(settingsPanel);
			
			JButton okBtn = new JButton("ok");
			okBtn.addActionListener(new OkBtnListener());
			JButton cancelBtn = new JButton(LANG.getCancel());
			cancelBtn.addActionListener(new CancelBtnListener());
			JPanel buttonPanel = new JPanel();
			buttonPanel.setBackground(GuiTools.BGCOLOR);
			buttonPanel.add(okBtn);
			buttonPanel.add(cancelBtn);
			this.getContentPane().add(BorderLayout.NORTH,new Header(((edit)?LANG.getEditTagAreaDefiningElement():LANG.getAddTagAreaDefiningElement())));
			this.getContentPane().add(BorderLayout.CENTER,centerPanel);
			this.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
			this.pack();
			this.setLocation(parentFrame.getLocationOnScreen());
			this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					imagePanel.setCursor(Cursor.getDefaultCursor());
					setTagAreaDefinitionElement=false;
				}
			});
			String type = (edit)?tade.getType():settings.get("type");
			if(type.equals("smallArrow")) smallArrow.doClick();
			if(type.equals("mediumArrow")) mediumArrow.doClick();
			if(type.equals("bigArrow")) bigArrow.doClick();
			if(type.equals("smallDelimiter")) smallDelimiter.doClick();
			if(type.equals("mediumDelimiter")) mediumDelimiter.doClick();
			if(type.equals("bigDelimiter")) bigDelimiter.doClick();
			if(!edit){
				String colo = settings.get("color");
				String[] split1 = colo.split(";");
				color = new Color(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]));
				String fColor = settings.get("flashColor");
				String[] split2 = fColor.split(";");
				flashColor = new Color(Integer.parseInt(split2[0]),Integer.parseInt(split2[1]),Integer.parseInt(split2[2]));
				colorPreview.setBackground(color);
				flashColorPreview.setBackground(flashColor);
			}
			this.setVisible(true);
		}
		
		private class OkBtnListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				tadeColor=color;
				tadeFlashColor=flashColor;
				tadeType=type;
				if(edit){
					tade.setColor(tadeColor);
					tade.setFlashColor(tadeFlashColor);
					tade.setType(type);
					PED.setTagArm(markTagIndex,editTagIndex,editArmIndex,tade);
				}
				else{
					setTagAreaDefinitionElement=true;
					imagePanel.setCursor(addTADECursor);
				}
				settings.put("rememberSettings",(rememberSettingsCB.isSelected())?"true":"false");
				if(rememberSettingsCB.isSelected()){
					settings.put("type", tadeType);
					settings.put("color",color.getRed()+";"+color.getGreen()+";"+color.getBlue());
					settings.put("flashColor",flashColor.getRed()+";"+flashColor.getGreen()+";"+flashColor.getBlue());
				}
				SET.setCreateTADESettings(settings);
				dialog.dispose();
			}
		}
		
		private class CancelBtnListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				imagePanel.setCursor(Cursor.getDefaultCursor());
				setTagAreaDefinitionElement=false;
				dialog.dispose();
			}
		}
		
		private class CbListener implements ActionListener{
			String typeSelection;
			public CbListener(String t){
				typeSelection=t;
			}
			
			public void actionPerformed(ActionEvent e){
				type=typeSelection;
			}
		}
		
		private class ColorBtnListener implements ActionListener{
			
			public void actionPerformed(ActionEvent e){
					Color col=JColorChooser.showDialog(dialog, LANG.getShapeColor(), color);
					color=col==null?color:col;
					colorPreview.setBackground(color);
			}
		}
		
		private class FlashColorBtnListener implements ActionListener{
			
			public void actionPerformed(ActionEvent e){
					Color col=JColorChooser.showDialog(dialog, LANG.getShapeColor(), flashColor);
					flashColor=col==null?flashColor:col;
					flashColorPreview.setBackground(flashColor);
			}
		}
	}
}
